package com.sharekitchen.mapper;

import com.sharekitchen.dto.DeviceDto;
import com.sharekitchen.entity.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
@Transactional
public interface DeviceMapper {

    //    查询所有
    @Select("SELECT * FROM `device` WHERE is_delete=0 limit #{start},#{pageSize} ")
    List<Device> getAllDev(int start, int pageSize);

    @Select("SELECT * FROM `device_images` WHERE is_delete=0")
    List<DeviceImages> getAllDevImg();

    @Select("SELECT * FROM `device_detail` WHERE is_delete=0")
    List<DeviceDetail> getAllDevDetail();

    @Select("SELECT * FROM `device` WHERE is_delete=0 AND dev_id=#{devId}")
    Device getDevById(String devId);

    @Select("SELECT * FROM `device_images` WHERE is_delete=0 AND dev_id=#{devId}")
    DeviceImages getDevImgById(String devId);

    @Select("SELECT * FROM `device_detail` WHERE is_delete=0 AND dev_id=#{devId}")
    DeviceDetail getDevDetailById(String devId);

    //    添加设备
    @Insert("INSERT INTO `device` VALUES (#{devId},#{devName},#{devDesc},#{isDelete})")
    int addDevice(DeviceDto deviceDto);

    @Insert("INSERT INTO `device_images` VALUES (#{devUrl},#{devId},#{isDelete})")
    int addDeviceImages(DeviceImages deviceDto);

    @Insert("INSERT INTO `device_detail` VALUES (#{devId},#{devNum},#{devUsable},#{devUsing},#{isDelete})")
    int addDeviceDetail(DeviceDetail deviceDto);

//    删除

    @Update("UPDATE `device` SET is_delete=1 WHERE dev_id=#{devId} AND is_delete=0")
    int deleteDevice(String devId);

    @Update("UPDATE `device_images` SET is_delete=1 WHERE dev_id=#{devId} AND is_delete=0")
    int deleteDeviceImages(String devId);

    @Update("UPDATE `device_detail` SET is_delete=1 WHERE dev_id=#{devId} AND is_delete=0")
    int deleteDeviceDetail(String devId);

//    修改

    @Update("UPDATE `device` SET dev_name=#{devName},dev_desc=#{devDesc} WHERE dev_id=#{devId} AND is_delete=0")
    boolean updateDevice(DeviceDto deviceDto);

    @Update("UPDATE `device_detail` SET  dev_num=#{devNum},dev_usable=#{devUsable},dev_using=#{devUsing} WHERE dev_id=#{devId} AND is_delete=0")
    boolean updateDeviceDetail(DeviceDetail deviceDetail);

    @Select("SELECT count(*) FROM `device` WHERE is_delete = 0")
    int getTotalCount();

    @Select("SELECT SUM(dev_num)as devNum ,SUM(dev_using)as devUsing,SUM(dev_usable) as devUsable FROM `device_detail` WHERE is_delete=0 ")
    DeviceNum getDeviceNum();
}
